package com.ruoyi.project.sysmanage.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.sysmanage.domain.Cost;
import com.ruoyi.project.sysmanage.service.CostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : 代学欣
 * @date : 20:46 2021/7/13
 */
@RestController
@RequestMapping("/sysmanage/cost")
public class CostController extends BaseController {
    @Autowired
    private CostService costService;

    /**
     * 获取所有检查费用列表
     */
    @PreAuthorize("@ss.hasPermi('sysmanage:cost:query')")
    @GetMapping("list")
    public TableDataInfo list(Cost cost){
        //分页查询,处理请求参数pageNum,pageSize等。并使用PageHelper处理分页查询
        startPage();
        //调用service查询数据库
        List<Cost> list = costService.selectList(cost);
        //返回
        return getDataTable(list);
    }

    /**
     * 新增检查费用
     */
    @PreAuthorize("@ss.hasPermi('sysmanage:cost:add')")
    @PostMapping
    public AjaxResult addCost(@RequestBody Cost cost){
        cost.setCreateBy(SecurityUtils.getUsername());
        int rows = costService.addCost(cost);
        return toAjax(rows);
    }

    /**
     * 修改前的查询显示,获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('sysmanage:cost:query')")
    @GetMapping("/{item_id}")
    public AjaxResult getById(@PathVariable Integer item_id){
        Cost cost = costService.getById(item_id);
        return AjaxResult.success(cost);
    }

    /**
     * 修改检查费用参数
     * @param cost
     * @return
     */
    @PreAuthorize("@ss.hasPermi('sysmanage:cost:update')")
    @PutMapping
    public AjaxResult editCost(@RequestBody Cost cost){
        cost.setUpdateBy(SecurityUtils.getUsername());
        int rows = costService.updateCost(cost);
        return toAjax(rows);
    }

    /**
     * 检查费用的删除
     * @param item_ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('sysmanage:cost:delete')")
    @DeleteMapping("/{item_ids}")
    public AjaxResult romoveCost(@PathVariable Integer[] item_ids){
        int rows = costService.deleteCost(item_ids);
        return toAjax(rows);
    }

}
